using Cysharp.Threading.Tasks;
using MoonFramework.FSM;
using Tool.Name;
using UnityEngine;

namespace Entity.Monster.State
{
    public class MonsterWalkState : WalkState , IMonster
    {
        public Monster Monster => _actor as Monster;

        protected override async UniTaskVoid Walk()
        {
            while (_isRun)
            {
                await UniTask.Yield();
                try
                {
                    if (Vector3.Distance(_actor.viewController.transform.position, _actor.viewController.targetDir) >=
                          Monster.MonsterConfig.Radius) continue;
                    Monster.fsmMachine.ChangeState(nameof(MonsterIdleState));
                    Monster.MonsterObject.NavMeshAgent.enabled = false;
                }
                catch
                {
                    //防止物体已经卸载但是异步任务没有结束
                }
            }
        }

        protected override void FootStep()
        {
            index %= 2;
            _actor.viewController.PlayAudio(index == 0? MonsterAudioName.WalkLeft : 
                    MonsterAudioName.WalkRight, 
                0.25f);
            index++;
        }
    }
}